package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.configInfo.ConfigInfo;
import com.hornsun.domain.constant.types.Whether;
import com.hornsun.domain.dbo.Commodity;
import com.hornsun.domain.dbo.CommodityExample;
import com.hornsun.repository.mapper.CommodityMapper;
import com.hornsun.util.LittleUtil;
import com.hornsun.util.StrUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/3/2.
 */
@Repository
public class CommodityRepository {

    @Resource
    private CommodityMapper commodityMapper;

    @Autowired
    private ConfigInfo configInfo;

    @Autowired
    private LittleUtil littleUtil;

    public List<Commodity> queryCommodityInfo(String typeid,int page){
        CommodityExample commodityExample = new CommodityExample();
        CommodityExample.Criteria criteria  = commodityExample.createCriteria();
        criteria.andTypeidEqualTo(typeid);
        commodityExample.setOrderByClause(StrUtil.toDesc("sell_time"));
        return commodityMapper.selectByExampleWithRowbounds(commodityExample,new RowBounds((page-1)*configInfo.getLimit(),configInfo.getLimit()));
    }

    public void updateCommodity(Commodity commodity){
        int count = commodityMapper.updateByPrimaryKeySelective(commodity);
        if (count!=1){
            throw new IllegalArgumentException("update commodity failed by commodity:"+commodity);
        }
    }

    /**
     * 插入商品
     * @param commodity
     */
    public void insertCommodity(Commodity commodity){
        int count = commodityMapper.insertSelective(commodity);
        if (count!=1){
            throw new IllegalArgumentException("insert commodity failed by commodity:"+commodity);
        }
    }

    public void deleteCommodity(@RequestParam("guid") String guid){
        int count = commodityMapper.deleteByPrimaryKey(guid);
        if (count!=1){
            throw new IllegalArgumentException("delete commodity failed by guid:"+guid);
        }
    }

    public List<Commodity> getHomeRecommendCommodity(){
        CommodityExample commodityExample = new CommodityExample();
        commodityExample.setOrderByClause(StrUtil.toDesc("sell_time"));
        CommodityExample.Criteria criteria = commodityExample.createCriteria();
        criteria.andHomepangRecommendEqualTo((byte) Whether.YES.getType().intValue());
        return commodityMapper.selectByExample(commodityExample);

    }

//获取收藏商品信息
    public List<Commodity> getFavouriteCommodity(String openid , int page){
        Preconditions.checkNotNull(openid);
        Preconditions.checkNotNull(page);
        int offset  = (page-1)*8;
        return commodityMapper.getFavouriteCommodity(openid,offset);
    }

    public Commodity getCommodityByGuid(String commodityGuid){
        Preconditions.checkNotNull(commodityGuid);
        return commodityMapper.selectByPrimaryKey(commodityGuid);
    }

    //获取最近两条商品信息
    public List<Commodity> getLatelyTwo(){
        CommodityExample commodityExample = new CommodityExample();
        commodityExample.setOrderByClause(StrUtil.toDesc("sell_time"));
        return commodityMapper.selectByExampleWithRowbounds(commodityExample,new RowBounds(0,2));
    }


    //获取商品总条数
    public Long getCountByTypeid(String typeid){
        CommodityExample commodityExample = new CommodityExample();
        CommodityExample.Criteria criteria = commodityExample.createCriteria();
        criteria.andTypeidEqualTo(typeid);
        return commodityMapper.countByExample(commodityExample);
    }

    //该类型是否有内容
        public boolean hasContentByTypeId(String typeId) {
        CommodityExample commodityExample = new CommodityExample();
        CommodityExample.Criteria criteria = commodityExample.createCriteria();
        criteria.andTypeidEqualTo(typeId);
        long count = commodityMapper.countByExample(commodityExample);
        if (count > 0) {
            return true;
        }
        return false;
    }

    //获取所有商品信息
    public List<Commodity> getALL(int page){
        CommodityExample commodityExample = new CommodityExample();
        commodityExample.setOrderByClause(StrUtil.toDesc("sell_time"));
        return commodityMapper.selectByExampleWithRowbounds(commodityExample,littleUtil.littleRowBounds(page));
    }

    //获取商品总数
    public long getTotalCount(){
        return commodityMapper.countByExample(new CommodityExample());
    }


    //模糊查询
    public List<Commodity> fuzzyQuery(String description,int page){
        CommodityExample commodityExample = new CommodityExample();
        commodityExample.setOrderByClause(StrUtil.toDesc("sell_time"));
        CommodityExample.Criteria criteria = commodityExample.createCriteria();
        criteria.andDescriptionLike(description);
        return commodityMapper.selectByExampleWithRowbounds(commodityExample,littleUtil.littleRowBounds(page));
    }


    //模糊查询的数量
    public long countByFuzzyQuery(String description){
        CommodityExample commodityExample = new CommodityExample();
        CommodityExample.Criteria criteria = commodityExample.createCriteria();
        criteria.andDescriptionLike(description);
        return commodityMapper.countByExample(commodityExample);
    }

    //更改商品收藏数
    public void updateFavourite(String commodityGuid){
        Preconditions.checkNotNull(commodityGuid);
        commodityMapper.updateFavourite(commodityGuid);
    }

}
